import request from '../../utils/request';

// 合同信息接口
export interface ContractVO {
  id?: number;
  name: string;
  customerId: number;
  customerName?: string;
  businessId?: number;
  businessName?: string;
  processInstanceId?: string;
  orderDate: Date;
  ownerUserId: number;
  ownerUserName?: string;
  no?: string;
  startTime?: Date;
  endTime?: Date;
  price?: number;
  discountPercent?: number;
  productPrice?: number;
  roUserIds?: string;
  rwUserIds?: string;
  contactId?: number;
  contactName?: string;
  signUserId?: number;
  signUserName?: string;
  contactLastTime?: Date;
  remark?: string;
  auditStatus?: number;
  createTime?: Date;
  updateTime?: Date;
}

// 合同产品信息接口
export interface ContractProductVO {
  id?: number;
  contractId: number;
  productId: number;
  productName?: string;
  productNo?: string;
  productUnit?: string;
  productPrice?: number;
  businessPrice?: number;
  count?: number;
  totalPrice?: number;
}

// 合同分页查询参数
export interface ContractPageParams {
  pageNo?: number;
  pageSize?: number;
  name?: string;
  customerId?: number;
  businessId?: number;
  ownerUserId?: number;
  auditStatus?: number;
  sceneType?: number;
}

// 合同 API
export const ContractApi = {
  // 查询合同分页
  getContractPage: async (params: ContractPageParams) => {
    return await request.get('/crm/contract/page', { params });
  },

  // 查询合同详情
  getContract: async (id: number) => {
    return await request.get(`/crm/contract/${id}`);
  },

  // 新增合同
  createContract: async (data: ContractVO) => {
    return await request.post('/crm/contract', data);
  },

  // 修改合同
  updateContract: async (data: ContractVO) => {
    return await request.put('/crm/contract', data);
  },

  // 删除合同
  deleteContract: async (id: number) => {
    return await request.delete(`/crm/contract/${id}`);
  },

  // 提交审核
  submitContract: async (id: number) => {
    return await request.put(`/crm/contract/${id}/submit`);
  },

  // 转移合同
  transferContract: async (ids: number[], newOwnerUserId: number) => {
    return await request.put('/crm/contract/transfer', { ids, newOwnerUserId });
  },

  // 获取合同简单列表
  getContractSimpleList: async () => {
    return await request.get('/crm/contract/simple-list');
  },

  // 导出合同
  exportContract: async (params: ContractPageParams) => {
    return await request.get('/crm/contract/export', { params, responseType: 'blob' });
  }
};

// 合同产品 API
export const ContractProductApi = {
  // 查询合同产品列表
  getContractProductList: async (contractId: number) => {
    return await request.get('/crm/contract-product/list', { params: { contractId } });
  },

  // 新增合同产品
  createContractProduct: async (data: ContractProductVO) => {
    return await request.post('/crm/contract-product', data);
  },

  // 修改合同产品
  updateContractProduct: async (data: ContractProductVO) => {
    return await request.put('/crm/contract-product', data);
  },

  // 删除合同产品
  deleteContractProduct: async (id: number) => {
    return await request.delete(`/crm/contract-product/${id}`);
  }
};